﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace Microhard.Framework.MVCHelper.Mobile
{
    public class MobileAutoComplate : IScriptableComponent
    {
        private string _controllerName;
        private string _actionName;
        public string AreaName { set; get; }

        public string AssetKey
        {
            set;
            get;
        }

        public string GridID
        {
            set;
            get;
        }

        public string ID
        {
            set;
            get;
        }

        public Dictionary<string, string> PassToActionValues
        {
            set;
            get;
        }

        public string WriteJQueryReadyInnerScript(System.Web.HttpContextBase helper)
        {
            StringBuilder js = new StringBuilder();
            js.AppendLine("$(\"#" + this.ID + "\" ).on(\"filterablebeforefilter\", function ( e, data ) {");
            js.AppendLine("var $ul = $( this ),");
            js.AppendLine("$input = $( data.input ),");
            js.AppendLine("value = $input.val(),");
            js.AppendLine("html = \"\";");
            js.AppendLine("$ul.html( \"\" );");
            js.AppendLine("if ( value && value.length > 1 ) {");
            js.AppendLine("$ul.html( \"<li><div class='ui-loader'><span class='ui-icon ui-icon-loading'></span></div></li>\" );");
            js.AppendLine("$ul.listview(\"refresh\");");
            js.AppendLine("$.ajax({");
            string url = "~/" + this._controllerName + "/" + this._actionName;
            if (!string.IsNullOrEmpty(this.AreaName))
            {
                url = "~/" + this.AreaName + "/" + this._controllerName + "/" + this._actionName;
            }
            url = UrlHelper.GenerateContentUrl(url, helper);

            js.AppendLine("url: \"" + url + "\",");
            js.AppendLine("data: {");
            js.AppendLine("q: $input.val()");
            js.AppendLine("}");
            js.AppendLine("})");
            js.AppendLine(".then( function ( response ) {");
            js.AppendLine("$.each( response, function ( i, val ) {");
            js.AppendLine("html += \"<li>\" + val + \"</li>\";");
            js.AppendLine("});");
            js.AppendLine("$ul.html( html );");
            js.AppendLine("$ul.listview(\"refresh\");");
            js.AppendLine("$ul.trigger(\"updatelayout\");");
            js.AppendLine("});");
            js.AppendLine("}");
            js.AppendLine("});");
            return js.ToString();
        }

        public string WriteJQueryReadyOutterScript(System.Web.HttpContextBase helper)
        {
            return null;
        }

        public System.Web.Mvc.MvcHtmlString WriteHtml(System.Web.Mvc.HtmlHelper helper, string linkText, string actionName, string controllerName)
        {
            this._actionName = actionName;
            this._controllerName = controllerName;
            string format = "<ul id=\"" + this.ID + "\" data-role=\"listview\" data-inset=\"true\" data-filter-placeholder=\"" + linkText + "\" data-filter=\"true\" data-filter-theme=\"a\"></ul>";
            return new System.Web.Mvc.MvcHtmlString(format);
        }
    }
}
